package com.cskaoyan.service.front.footprint;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.admin.BaseDataVo;
import com.cskaoyan.bean.vo.front.footprint.FootprintVo;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.ParamUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 浏览足迹服务impl
 *
 * @author 李然
 * @date 2022/10/14
 */
@Service
public class FootPrintServiceImpl implements FootPrintService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public BaseDataVo getFootprintList(BaseParam param) {
//        从session域中获取用户信息
        //        获取用户名

        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
//        if (username == null || "".equals(username)) {
//            return null;
//        }
        if(!ParamUtils.isValid(username)) {
            return null;
        }
        MarketUserExample userExample = new MarketUserExample();
        userExample.createCriteria().andUsernameEqualTo(username);

        // TODO: 2022/10/14 是否需要静态注入
        /**
         * solution:
         *      private static UserMapper staticUserMapper;
         *      staticUserMapper = this.userMapper;
         */
        List<MarketUser> users = marketUserMapper.selectByExample(userExample);

        //拿到具体用户的登录信息
        MarketUser marketUser = users.get(0);

//        通过criteria查询并按照更新时间对浏览足迹进行排序
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        footprintExample.createCriteria().andUserIdEqualTo(marketUser.getId());
        List<FootprintVo> footlist = new ArrayList<>();
//        pageHelp
        PageHelper.startPage(param.getPage(), param.getLimit());
//        通过更新时间降序排列
        footprintExample.setOrderByClause("update_time desc");
        List<MarketFootprint> footprintList = marketFootprintMapper.selectByExample(footprintExample);

//        循环遍历
        for (MarketFootprint footprint:
                footprintList
             ) {
//            通过足迹商品ID获取商品信息
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(footprint.getGoodsId());
            FootprintVo footprintVo = new FootprintVo(marketGoods.getBrief(),
                    marketGoods.getPicUrl(),
                    footprint.getAddTime(),
                    marketGoods.getId(),
                    marketGoods.getName(),
                    footprint.getId(),
                    marketGoods.getRetailPrice());
            footlist.add(footprintVo);
        }

//        pageInfo
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(footprintList);

        return new BaseDataVo(pageInfo,footlist);
    }

    @Override
    public int deleteFootPrint(Integer id) {
        MarketFootprint footprint = new MarketFootprint();
        footprint.setId(id);
        footprint.setDeleted(true);
        footprint.setUpdateTime(new Date());
        marketFootprintMapper.updateByPrimaryKeySelective(footprint);

        return 0;
    }
}
